<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="全宗号" prop="familyId">
				<el-select v-model="queryParams.familyId" filterable placeholder="请选择全宗号">
					<el-option v-for="item in familyOptions" :key="item.familyId" :label="item.familyName" :value="item.familyId"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="盒号" prop="boxId">
				<el-select v-model="queryParams.boxId" filterable placeholder="请选择盒号">
					<el-option v-for="item in boxOptions" :key="item.boxId" :label="item.boxCode" :value="item.boxId"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="卷宗号" prop="dossierId">
				<el-select v-model="queryParams.dossierId" filterable placeholder="请选择卷宗号">
					<el-option v-for="item in dossierOptions" :key="item.dossierId" :label="item.dossierName" :value="item.dossierId"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="目录名称" prop="catalogId">
				<el-tree-select v-model="queryParams.catalogId" :data="catalogOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
				 value-key="id" placeholder="请选择目录名称" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="所属库房" prop="storehouseId">
				<el-tree-select v-model="queryParams.storehouseId" :data="storehouseOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
				 value-key="id" placeholder="请选择库房名称" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档号" prop="archiveCode">
				<el-input v-model="queryParams.archiveCode" placeholder="请输入档号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="题名" prop="titleName">
				<el-input v-model="queryParams.titleName" placeholder="请输入题名" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="年度" prop="year">
				<el-input v-model="queryParams.year" placeholder="请输入年度" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="保管年限" prop="saveYear">
				<el-select v-model="queryParams.saveYear" placeholder="请选择保管年限" clearable>
					<el-option v-for="dict in save_year" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="密级" prop="secretLevel">
				<el-select v-model="queryParams.secretLevel" placeholder="请选择密级" clearable>
					<el-option v-for="dict in secret_level" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="鉴定状态" prop="appraisal">
				<el-select v-model="queryParams.appraisal" placeholder="请选择鉴定状态" clearable>
					<el-option v-for="dict in appraisal" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="借阅状态" prop="borrowflag">
				<el-select v-model="queryParams.borrowflag" placeholder="请选择借阅状态" clearable>
					<el-option v-for="dict in borrow_flag" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="件号" prop="pieceCode">
				<el-input v-model="queryParams.pieceCode" placeholder="请输入件号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="标签号" prop="rfidcode">
				<el-input v-model="queryParams.rfidcode" placeholder="请输入标签号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="密集架id" prop="shelvesId">
				<el-input v-model="queryParams.shelvesId" placeholder="请输入密集架id" clearable @keyup.enter="handleQuery" />
			</el-form-item> -->
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['archive:archive:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['archive:archive:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['archive:archive:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['archive:archive:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="archiveList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="全宗号" align="center" prop="family.familyCode" />
			<el-table-column label="盒号" align="center" prop="box.boxCode" />
			<el-table-column label="卷宗号" align="center" prop="dossier.dossierCode" />
			<el-table-column label="目录名称" align="center" prop="catalog.catalogName" />
			<!-- <el-table-column label="库房id" align="center" prop="storehouseId" />
			<el-table-column label="所属库房" align="center" prop="storehouse.storehouseName" /> -->
			<el-table-column label="年度" align="center" prop="year" />
			<el-table-column label="件号" align="center" prop="pieceCode" />
			<el-table-column label="档号" width="200" align="center" prop="archiveCode" />
			<el-table-column label="题名" width="400" align="center" prop="titleName" />
			<el-table-column label="借阅状态" align="center" prop="borrowflag">
				<template #default="scope">
					<dict-tag :options="borrow_flag" :value="scope.row.borrowflag" />
				</template>
			</el-table-column>
			<el-table-column label="档案状态" align="center" prop="status">
				<template #default="scope">
					<dict-tag :options="archive_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="页号" align="center" prop="pageNos" />
			<el-table-column label="保管年限" align="center" prop="saveYear">
				<template #default="scope">
					<dict-tag :options="save_year" :value="scope.row.saveYear" />
				</template>
			</el-table-column>
			<el-table-column label="密级" align="center" prop="secretLevel">
				<template #default="scope">
					<dict-tag :options="secret_level" :value="scope.row.secretLevel" />
				</template>
			</el-table-column>
			<el-table-column label="责任者" width="200" align="center" prop="duty" />
			<el-table-column label="机构或问题" align="center" prop="matter" />
			<el-table-column label="文号" align="center" prop="fileCode" />
			<el-table-column label="更新时间" align="center" prop="updateTime" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="鉴定状态" align="center" prop="appraisal">
				<template #default="scope">
					<dict-tag :options="appraisal" :value="scope.row.appraisal" />
				</template>
			</el-table-column>
			<el-table-column label="文件名" width="500" align="center" prop="fileName" />

			<el-table-column label="添加时间" align="center" prop="addTime" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="标签号" align="center" prop="rfidcode" />
			<el-table-column label="密集架id" align="center" prop="shelvesId" />
			<el-table-column label="密集架编号" width="155" align="center" prop="shelvesCode" />
			<el-table-column label="密集架名称" width="155" align="center" prop="shelvesName" />
			<el-table-column label="在架状态" align="center" prop="shelvesStatus">
				<template #default="scope">
					<dict-tag :options="status" :value="scope.row.shelvesStatus" />
				</template>
			</el-table-column>
			<el-table-column label="层编号" align="center" prop="shelvesLayerGrid" />
			<el-table-column label="层名称" align="center" prop="shelvesLayerGridName" />
			<el-table-column label="操作" width="155" align="center" class-name="small-padding fixed-width" fixed="right">
				<template #default="scope">
					<el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['archive:archive:edit']">修改</el-button>
					<el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['archive:archive:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改档案对话框 -->
		<el-dialog :title="title" v-model="open" width="700px" append-to-body>
			<el-form ref="archiveRef" :model="form" :rules="rules" label-width="80px">
				<el-row>
					<el-col :span="12">
						<el-form-item label="卷宗号" prop="dossierId">
							<el-select v-model="form.dossierId" filterable placeholder="请选择卷宗号">
								<el-option v-for="item in dossierOptions" :key="item.dossierId" :label="item.dossierName" :value="item.dossierId"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="全宗号" prop="familyId">
							<el-select v-model="form.familyId" filterable placeholder="请选择全宗号">
								<el-option v-for="item in familyOptions" :key="item.familyId" :label="item.familyName" :value="item.familyId"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="目录名称" prop="catalogId">
							<el-tree-select v-model="form.catalogId" :data="catalogOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
							 value-key="id" placeholder="请选择目录名称" check-strictly />
						</el-form-item>
					</el-col>
					
					<el-col :span="12">
						<el-form-item label="盒号" prop="boxId">
							<el-select v-model="form.boxId" filterable placeholder="请选择盒号">
								<el-option v-for="item in boxOptions" :key="item.boxId" :label="item.boxCode" :value="item.boxId"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="年度" prop="year">
							<el-input v-model="form.year" placeholder="请输入年度" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="保管年限" prop="saveYear">
							<el-select v-model="form.saveYear" placeholder="请选择保管年限">
								<el-option v-for="dict in save_year" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="密级" prop="secretLevel">
							<el-select v-model="form.secretLevel" placeholder="请选择密级">
								<el-option v-for="dict in secret_level" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="责任者" prop="duty">
							<el-input v-model="form.duty" placeholder="请输入责任者" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="题名" prop="titleName">
							<el-input v-model="form.titleName" placeholder="请输入题名" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="件号" prop="pieceCode">
							<el-input v-model="form.pieceCode" placeholder="请输入件号" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="档号" prop="archiveCode">
							<el-input v-model="form.archiveCode" placeholder="请输入档号" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="文号" prop="fileCode">
							<el-input v-model="form.fileCode" placeholder="请输入文号" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="页号" prop="pageNos">
							<el-input v-model="form.pageNos" placeholder="请输入页号" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="机构或问题" prop="matter">
							<el-input v-model="form.matter" placeholder="请输入机构或问题" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="标签号" prop="rfidcode">
							<el-input v-model="form.rfidcode" placeholder="请输入标签号" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="文件名">
							<file-upload v-model="form.fileName" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="Archive">
	import {
		listArchive,
		getArchive,
		delArchive,
		addArchive,
		updateArchive
	} from "@/api/archive/archive";
	import {
		treeselect
	} from "@/api/base/catalog";

	import {
		treeselectHouse
	} from "@/api/deposit/storehouse";

	import {
		listFamily
	} from "@/api/base/family";

	import {
		listBox
	} from "@/api/base/box";
	
	import {
		listDossier
	} from "@/api/base/dossier";
	const {
		proxy
	} = getCurrentInstance();
	const {
		secret_level,
		appraisal,
		status,
		is_use,
		archive_status,
		is_delflag,
		borrow_flag,
		is_renew,
		save_year
	} = proxy.useDict('secret_level', 'appraisal', 'status', 'is_use', 'archive_status', 'is_delflag', 'borrow_flag',
		'is_renew', 'save_year');

	const archiveList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const familyOptions = ref([]);
	const dossierOptions = ref([]);
	const boxOptions = ref([]);
	const catalogOptions = ref(undefined);
	const storehouseOptions = ref(undefined);
	
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			familyId: null,
			boxId: null,
			dossierId: null,
			catalogId: null,
			storehouseId: null,
			archiveCode: null,
			titleName: null,
			year: null,
			saveYear: null,
			secretLevel: null,
			appraisal: null,
			borrowflag: null,
			pieceCode: null,
			rfidcode: null,
			shelvesId: null,
		},
		rules: {}
	});

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);

	/** 查询档案列表 */
	function getList() {
		loading.value = true;
		listArchive(queryParams.value).then(response => {
			archiveList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}
	/** 查询目录下拉树结构 */
	function getTreeselect() {
		treeselect().then(response => {
			catalogOptions.value = response.data;
		});
	};
	/** 初始化目录数据 */
	function initTreeData() {		// 判断目录的数据是否存在，存在不获取，不存在则获取
		if (catalogOptions.value === undefined) {
			treeselect().then(response => {
				catalogOptions.value = response.data;
			});
		}
	};

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			archiveId: null,
			familyId: null,
			boxId: null,
			dossierId: null,
			catalogId: null,
			storehouseId: null,
			archiveCode: null,
			fileCode: null,
			titleName: null,
			pageNos: null,
			year: null,
			saveYear: null,
			secretLevel: null,
			duty: null,
			matter: null,
			updateTime: null,
			appraisal: null,
			borrowflag: null,
			status: null,
			delflag: null,
			fileName: null,
			pieceCode: null,
			addTime: null,
			isUse: null,
			rfidcode: null,
			shelvesId: null,
			shelvesCode: null,
			shelvesName: null,
			shelvesStatus: null,
			shelvesLayerGrid: null,
			shelvesLayerGridName: null,
			isRenew: null,
			borrowUser: null,
			userPhone: null,
			planBorrowDate: null
		};
		proxy.resetForm("archiveRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.archiveId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		initTreeData();
		initTreeDataHouse();
		getArchive().then(response => {
			familyOptions.value = response.familys;
			dossierOptions.value = response.dossiers;
			boxOptions.value = response.boxs;

			open.value = true;
			title.value = "添加档案";
		});
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		initTreeData();
		initTreeDataHouse();
		const archiveId = row.archiveId || ids.value
		getArchive(archiveId).then(response => {
			familyOptions.value = response.familys;
			dossierOptions.value = response.dossiers;
			boxOptions.value = response.boxs;
			
			form.value = response.data
			console.log(form.value);
			open.value = true;
			title.value = "修改档案";
		});
	}
	// /** 修改按钮操作 */
	// function handleUpdate(row) {
	// 	reset();
	// 	initTreeData();
	// 	const archiveId = row.archiveId || ids.value
	// 	getDossier(archiveId).then(response => {
	// 		familyOptions.value = response.familys;
	// 		form.value = response.data;
	// 		open.value = true;
	// 		title.value = "修改卷宗管理";
	// 	});
	// }

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["archiveRef"].validate(valid => {
			if (valid) {
				if (form.value.archiveId != null) {
					updateArchive(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addArchive(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const archiveIds = row.archiveId || ids.value;
		proxy.$modal.confirm('是否确认删除档案编号为"' + archiveIds + '"的数据项？').then(function() {
			return delArchive(archiveIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/archive/export', {
			...queryParams.value
		}, `archive_${new Date().getTime()}.xlsx`)
	}

	/** 查询库房下拉树结构 */
	function getTreeselectStorehouse() {
		treeselectHouse().then(response => {
			storehouseOptions.value = response.data;
		});
	};


	/** 初始化库房数据 */
	function initTreeDataHouse() {
		// 判断库房的数据是否存在，存在不获取，不存在则获取
		if (storehouseOptions.value === undefined) {
			treeselectHouse().then(response => {
				storehouseOptions.value = response.data;
			});
		}
	};


	function getSelectBase() {
		getArchive().then(response => {
			familyOptions.value = response.familys;
			dossierOptions.value = response.dossiers;
			boxOptions.value = response.boxs;
		});
	};

	getSelectBase();
	getTreeselectStorehouse();
	getTreeselect();
	getList();
</script>
